package com.example.ssm.controller;

import com.example.ssm.bean.CommonResult;
import com.example.ssm.bean.Employee;
import com.example.ssm.bean.EmployeeExample;
import com.example.ssm.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Intellij IDEA.
 * User: JonnyJiang
 * Date: 2021/6/5
 */

@Controller
public class EmployeeController {

    @Resource
    EmployeeService employeeService;



    /**获取某页的职员
     * @param pn 先将数据库中的职员通过分页助手分页。然后获取某页的职员
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/empAll",method = RequestMethod.GET)
    public CommonResult getEmpAll(@RequestParam(value = "pn",defaultValue = "1")Integer pn){
        PageHelper.startPage(pn, 10);
        List<Employee> all = employeeService.getAll();
        PageInfo pageInfo = new PageInfo(all, 5);
        return new CommonResult(200,"ok").add("data",pageInfo);
    }

    /**
     * 添加职员
     * @param employee 需要添加的职员
     * @param result 返回的结果
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/emp", method = RequestMethod.POST)
    public CommonResult addEmp(@Valid Employee employee, BindingResult result){
        if (result.hasErrors()){
            Map<String,Object> map = new HashMap<>();
            List<FieldError> fieldErrors = result.getFieldErrors();
            System.out.println("错误信息："+fieldErrors);
            for (FieldError fieldError : fieldErrors) {
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            return new CommonResult(100,"error").add("fieldError",map);
        }else{
            System.out.println("跳过了");
            employeeService.addEmp(employee);
            return new CommonResult(200,"ok");
        }
    }

    /**
     * 查询某个职员
     * @param id  查询职员的id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/emp",method = RequestMethod.GET)
    public CommonResult getEmp(@RequestParam("empId")Integer id){
        Employee emp = employeeService.getEmp(id);
        if (emp != null){
            return new CommonResult(200,"ok").add("emp",emp);
        }else {
            return new CommonResult(100,"error");
        }
    }

    /**
     * 更新某个职员的信息
     * @param employee 需要更新的职员的信息。
     * @param result 保存本次操作的结果
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/emp",method = RequestMethod.PUT)
    public CommonResult updateEmp(@Valid Employee employee, BindingResult result){
        if (result.hasErrors()){
            Map<String,Object> map = new HashMap<>();
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            return new CommonResult(100,"error").add("fieldError",map);
        }else{
            employeeService.updateEmp(employee);
            return new CommonResult(200,"ok");
        }
    }

    /**
     * 删除某个职员
     * @param empId 删除职员得到id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/emp/{empId}",method = RequestMethod.DELETE)
    public CommonResult delEmp(@PathVariable("empId")String empId){

        int result = 0;
        if (empId.contains("-")){
            List<Integer> ids = new ArrayList<>();
            for (String s : empId.split("-")) {
                ids.add(Integer.valueOf(s));
            }
            employeeService.deleteEmpBatch(ids);
            return new CommonResult(200,"ok");
        }else{
            result = employeeService.deleteEmp(Integer.valueOf(empId));
            if (result > 0){
                return new CommonResult(200,"ok");
            }else {
                return new CommonResult(100,"error");
            }
        }
    }

    /**
     *检验数据库中是否已经存在该职员
     * @param empName 被检验职员的姓名
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/checkUser",method = RequestMethod.POST)
    public CommonResult hasEmp(@RequestParam("empName")String empName){
        String nameVld = "(^[a-zA-Z][a-zA-Z0-9_]{4,15}$)|(^[\\u4e00-\\u9fa5]{2,}[a-zA-Z0-9_]{0,10}$)";
        if(!empName.matches(nameVld)){
            return new CommonResult(100,"error").add("msg", "用户名不能以数字开头、不能为空且长度为6~20位或者2~6位中文！");
        }
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long l = employeeService.hasUser(employeeExample);
        boolean invalid = l < 1;
        if(invalid){
            return new CommonResult(200,"ok");
        }else{
            return new CommonResult(100,"error").add("msg","用户已存在");
        }
    }

}
